package cn.edu.gzgs.controller;

import cn.edu.gzgs.service.WebSocketMessageService;
import cn.edu.gzgs.vo.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * 消息发送控制器
 * 用于演示WebSocket消息推送功能
 */
@Tag(name = "消息推送接口", description = "WebSocket消息推送相关接口")
@RestController
@RequestMapping("/api/message")
@Slf4j
public class MessageController {

    @Autowired
    private WebSocketMessageService webSocketMessageService;

    /**
     * 发送项目审核通过通知
     */
    @Operation(summary = "发送项目通过通知", description = "发送项目审核通过的通知消息")
    @PostMapping("/project/pass")
    public ResponseEntity<ApiResponse<String>> sendProjectPassNotification(
            @Parameter(description = "用户ID") @RequestParam String userId,
            @Parameter(description = "项目名称") @RequestParam String projectName) {
        
        try {
            webSocketMessageService.sendProjectPassNotification(userId, projectName);
            log.info("项目通过通知发送成功: 用户={}, 项目={}", userId, projectName);
            
            return ResponseEntity.ok(ApiResponse.success("项目通过通知发送成功"));
            
        } catch (Exception e) {
            log.error("发送项目通过通知失败", e);
            return ResponseEntity.status(500)
                    .body(ApiResponse.error(500, "发送通知失败"));
        }
    }

    /**
     * 发送项目审核拒绝通知
     */
    @Operation(summary = "发送项目拒绝通知", description = "发送项目审核拒绝的通知消息")
    @PostMapping("/project/repulse")
    public ResponseEntity<ApiResponse<String>> sendProjectRepulseNotification(
            @Parameter(description = "用户ID") @RequestParam String userId,
            @Parameter(description = "项目名称") @RequestParam String projectName,
            @Parameter(description = "拒绝原因") @RequestParam String reason) {
        
        try {
            webSocketMessageService.sendProjectRepulseNotification(userId, projectName, reason);
            log.info("项目拒绝通知发送成功: 用户={}, 项目={}", userId, projectName);
            
            return ResponseEntity.ok(ApiResponse.success("项目拒绝通知发送成功"));
            
        } catch (Exception e) {
            log.error("发送项目拒绝通知失败", e);
            return ResponseEntity.status(500)
                    .body(ApiResponse.error(500, "发送通知失败"));
        }
    }

    /**
     * 发送私信
     */
    @Operation(summary = "发送私信", description = "发送私人消息")
    @PostMapping("/private")
    public ResponseEntity<ApiResponse<String>> sendPrivateMessage(
            @Parameter(description = "接收者ID") @RequestParam String acceptId,
            @Parameter(description = "消息标题") @RequestParam String title,
            @Parameter(description = "消息内容") @RequestParam String content) {
        
        try {
            // 获取当前用户信息
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String senderId = authentication.getName();
            // 这里应该从用户服务获取发送者姓名，暂时使用用户ID
            String senderName = senderId;
            
            webSocketMessageService.sendPrivateMessage(senderId, senderName, acceptId, title, content);
            log.info("私信发送成功: {} -> {}, 标题={}", senderId, acceptId, title);
            
            return ResponseEntity.ok(ApiResponse.success("私信发送成功"));
            
        } catch (Exception e) {
            log.error("发送私信失败", e);
            return ResponseEntity.status(500)
                    .body(ApiResponse.error(500, "发送私信失败"));
        }
    }

    /**
     * 发送通知消息
     */
    @Operation(summary = "发送通知消息", description = "发送通知类消息")
    @PostMapping("/inform")
    public ResponseEntity<ApiResponse<String>> sendInformMessage(
            @Parameter(description = "用户ID") @RequestParam String userId,
            @Parameter(description = "通知标题") @RequestParam String title,
            @Parameter(description = "通知内容") @RequestParam String content) {
        
        try {
            webSocketMessageService.sendInformMessage(userId, title, content);
            log.info("通知消息发送成功: 用户={}, 标题={}", userId, title);
            
            return ResponseEntity.ok(ApiResponse.success("通知消息发送成功"));
            
        } catch (Exception e) {
            log.error("发送通知消息失败", e);
            return ResponseEntity.status(500)
                    .body(ApiResponse.error(500, "发送通知失败"));
        }
    }

    /**
     * 发送系统消息
     */
    @Operation(summary = "发送系统消息", description = "发送系统级消息")
    @PostMapping("/system")
    public ResponseEntity<ApiResponse<String>> sendSystemMessage(
            @Parameter(description = "用户ID") @RequestParam String userId,
            @Parameter(description = "消息标题") @RequestParam String title,
            @Parameter(description = "消息内容") @RequestParam String content) {
        
        try {
            webSocketMessageService.sendSystemMessage(userId, title, content);
            log.info("系统消息发送成功: 用户={}, 标题={}", userId, title);
            
            return ResponseEntity.ok(ApiResponse.success("系统消息发送成功"));
            
        } catch (Exception e) {
            log.error("发送系统消息失败", e);
            return ResponseEntity.status(500)
                    .body(ApiResponse.error(500, "发送系统消息失败"));
        }
    }

    /**
     * 强制用户下线
     */
    @Operation(summary = "强制用户下线", description = "强制指定用户下线")
    @PostMapping("/offline")
    public ResponseEntity<ApiResponse<String>> forceUserOffline(
            @Parameter(description = "用户ID") @RequestParam String userId,
            @Parameter(description = "下线原因") @RequestParam(required = false) String reason) {
        
        try {
            String offlineReason = reason != null ? reason : "系统维护，强制下线";
            webSocketMessageService.forceUserOffline(userId, offlineReason);
            log.info("用户强制下线成功: 用户={}, 原因={}", userId, offlineReason);
            
            return ResponseEntity.ok(ApiResponse.success("用户强制下线成功"));
            
        } catch (Exception e) {
            log.error("强制用户下线失败", e);
            return ResponseEntity.status(500)
                    .body(ApiResponse.error(500, "强制下线失败"));
        }
    }
}